package com.example.front.service.impl;

import com.example.front.dao.CategoryDao;
import com.example.front.service.ICategoryService;
import com.example.gok_mall.entity.Category;
import com.example.gok_mall.reponseData.ResultData;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryDao categoryDao;

    //增加分类
    public ResultData addCategory(String categoryName, Integer parentId){
        if(parentId==null|| StringUtils.isBlank(categoryName)){
            return ResultData.fail("参数错误，增加品类失败");
        }
        Category category=new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);//true代表分类可用

        int  count= categoryDao.insert(category);
        if(count>0){
            return ResultData.success("新增品类成功");
        }
        return ResultData.fail("服务器错误，新增品类失败");
    }


    //更新修改分类
    public ResultData updateCategoryName(Integer categoryId,String categoryName){
        if(categoryId==null|| StringUtils.isBlank(categoryName)){
            return ResultData.fail("参数错误，更新品类失败");
        }
        Category category=new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        category.setUpdateTime(new Date());
        int RowCount= categoryDao.updateByPrimaryKeySelective(category);

        if(RowCount>0){
            return ResultData.success("更新品类名字成功");
        }
        return ResultData.fail("服务器错误，更新品类字失败");

    }

    //获取品类子节点(平级)
    public ResultData<List<Category>> getChildrenParallelCategory(Integer categoryId){
        List<Category> categoryList=categoryDao.selectCategoryChildrenByParentId(categoryId);
        if(CollectionUtils.isEmpty(categoryList)){
            log.info("未找到当前分类的子分类");
        }
        return ResultData.createData(categoryList);
    }

    /**
     * 递归查询查询该节点和子节点的Id
     * @param categoryId
     * @return
     */
    public ResultData<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){

        //调用递归算法
        Set<Category> categorySet= Sets.newHashSet();
        finChildCategory(categorySet,categoryId);


        List<Integer> categoryIdList= Lists.newArrayList();
        if(categoryId !=null){
            for(Category categoryItem : categorySet){
                categoryIdList.add(categoryItem.getId());
            }
        }
        return ResultData.createData(categoryIdList);
    }

    //递归算法算出子节点
    private Set<Category> finChildCategory(Set<Category> categorySet,Integer categoryId){
        Category category=categoryDao.selectByPrimaryKey(categoryId);
        if(category !=null){
            categorySet.add(category);
        }
        //查找子节点，递归算法一定要有一个退出条件，当子节点不再有的时候，就跳出递归
        List<Category> categoryList=categoryDao.selectCategoryChildrenByParentId(categoryId);
        for(Category categoryItem:categoryList){
            finChildCategory(categorySet,categoryItem.getId());
        }
        return categorySet;
    }
}
